package com.lujianfei.productprice.ui.cart;

import com.lujianfei.productprice.ui.main.ShopEntity;
import com.lujianfei.productprice.ui.addProduct.ProductEntity;
import com.lujianfei.productprice.ui.order.OrderDetailEntity;
import com.lujianfei.productprice.ui.order.OrderEntity;

import java.util.List;

public class CartModel implements CartContract.Model {

    List<CartEntity> cartEntities;
    @Override
    public List<CartEntity> loadData() {
        cartEntities = CartEntity.listAll(CartEntity.class);
        if (null!=cartEntities){
            for (CartEntity cart :
                    cartEntities) {
                ProductEntity productEntity = ProductEntity.findById(ProductEntity.class, cart.getProductId());
                ShopEntity shopEntity = ShopEntity.findById(ShopEntity.class, productEntity.getShopId());
                cart.setShopName(shopEntity.getShopName());
                cart.setProductName(productEntity.getProductName());
                cart.setProductPrice(productEntity.getPrice());
            }
        }
        return cartEntities;
    }

    @Override
    public void addQuantity(CartEntity data) {
        data.setQuantity(data.getQuantity()+1);
        data.save();
    }

    @Override
    public void minusQuantity(CartEntity data) {
        if (data.getQuantity()>=2) {
            data.setQuantity(data.getQuantity() - 1);
            data.save();
        }
    }

    @Override
    public List<CartEntity> getData() {
        return cartEntities;
    }

    @Override
    public double getTotalPrice() {
        double totalPrice = 0;
        for (CartEntity cartEntity:
        cartEntities) {
            totalPrice += (double)cartEntity.getQuantity() * cartEntity.getProductPrice();
        }
        return totalPrice;
    }

    @Override
    public boolean submit() {
        if(null == cartEntities || cartEntities.isEmpty()){
            return false;
        }
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setCreateTime(System.currentTimeMillis());
        long orderId = orderEntity.save();
        double totalPrice = 0;
        for (CartEntity cartEntity : cartEntities){
            OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
            orderDetailEntity.setOrderId(orderId);
            orderDetailEntity.setShopId(cartEntity.getShopId());
            orderDetailEntity.setProductId(cartEntity.getProductId());
            orderDetailEntity.setProductName(cartEntity.getProductName());
            orderDetailEntity.setPrice(cartEntity.getProductPrice());
            orderDetailEntity.setShopName(cartEntity.getShopName());
            orderDetailEntity.setQuantity(cartEntity.getQuantity());
            orderDetailEntity.setTotalPrice(cartEntity.getProductPrice() * (double)cartEntity.getQuantity());
            orderDetailEntity.save();
            totalPrice += orderDetailEntity.getTotalPrice();
        }
        orderEntity.setTotalPrice(totalPrice);
        orderEntity.setId(orderId);
        orderEntity.save();
        return true;
    }

    @Override
    public void clearCart() {
        if(null == cartEntities || cartEntities.isEmpty()){
            return;
        }
        for (CartEntity cartEntity : cartEntities){
            cartEntity.delete();
        }
    }

    @Override
    public void delete(CartEntity cartEntity) {
        cartEntity.delete();
    }
}
